import KoaRouter from 'koa-router'
import { Models } from '../../../common/typings/model'
import { Api } from '../../../common/typings/api'
import Result from '../../../common/utils/Result'
import Mysql from '../../../core/Mysql'
import registerSchema from '../../../common/apiValidate/user/register'
import validator from '../../../middlewares/validator'
import sedEmail from '../../../common/utils/Email'
import httpErrors from '../../../core/HttpException'
import bcrypt from "bcryptjs"

const router = new KoaRouter({
    prefix:'/api/v1/user'
})

/**
 * 邮箱注册，注册成功后，权限为NoVerifyEmail，并发送验证邮件，验证通过后权限为User
 */
router.get('/emailRegister.do',validator(registerSchema),async (ctx: Models.Ctx,next:Function)=> {

    let query: Api.User.Register.emailRegister = ctx.request.query as unknown as Api.User.Register.emailRegister
    

    // const sql = `SELECT
    //             *
    //             FROM
    //             poetry as p
    //             WHERE
    //             p.title LIKE '%李%'
    //             LIMIT 1,
    //             10`
    // const res = await Mysql.query(sql)
    // console.log(res)
    // const {
    //     userName
    // } = ctx.query

    // 注册

    // 发送邮件
    const res = await sedEmail({
        to: query.email,
        subject: '验证码',
        text:'验证码',
        html:`
            <div method="post" action="${origin}/api/v1/user/emailRegister.do">
                您于近期注册了xxx帐号${query.email}, 
                请<a 
                href="${origin}/api/v1/user/emailRegister.do?email=${query.email}&"
                style="color: green;font-weight: 600;margin: 0 6px"
                >点击验证</a>完成注册。
            </div>
        `,
    })
    
    throw new httpErrors.Success()
})

router.post('/sendRegisterEmail.do',validator(registerSchema,'body'),async (ctx: Models.Ctx,next:Function)=> {

    let query: Api.User.Register.emailRegister = ctx.request.body as unknown as Api.User.Register.emailRegister
    let origin = ctx.origin
    // const sql = `SELECT
    //             *
    //             FROM
    //             poetry as p
    //             WHERE
    //             p.title LIKE '%李%'
    //             LIMIT 1,
    //             10`
    // const res = await Mysql.query(sql)
    // console.log(res)
    // const {
    //     userName
    // } = ctx.query
    let password = ''
    for(let i =0;i<query.password.length;i++){
        password += query.password[i].charCodeAt(0)
        if(i < query.password.length - 1){
            password += '；'
        }
    }
    const res = await sedEmail({
        to: query.email,
        subject: '验证码',
        text:'验证码',
        html:`
            <div method="post" action="${origin}/api/v1/user/emailRegister.do">
                您于近期注册了xxx帐号${query.email}, 
                请<a 
                href="${origin}/api/v1/user/emailRegister.do?email=${query.email}&"
                style="color: green;font-weight: 600;margin: 0 6px"
                >点击验证</a>完成注册。
            </div>
        `,
    })
    throw new httpErrors.Success(res)
})

export default router